import unittest
import sys, os
from os.path import join, dirname
location = join(dirname(sys.argv[0]), '..')

sys.path.insert(0, location)

import port_parser
import package
from package import PackageKey


class StaticStrings(object):
    tetex_teststring = '''teTeX 3.0, Revision 3, print/teTeX (Variants: nox11)
http://www.tug.org/teTeX/

teTeX is a TeX distribution for UNIX compatible systems. It contains the latest \
versions of TeX & friends and nearly everything you need for happy TeX'ing. For \
more information have a look at the lengthy FEATURES file of the distribution.

Build Dependencies: gawk, gsed
Library Dependencies: XFree86
Runtime Dependencies: ghostscript, texinfo, texi2html
Platforms: darwin
Maintainers: gwright@opendarwin.org
'''

    php5_teststring = '''php5 5.2.1, www/php5 (Variants: darwin_6, darwin_7, \
darwin_8, freebsd, macosx, apache, apache2, fastcgi, snmp, macports_snmp, mysql3, \
mysql4, mysql5, postgresql, ipc, pcntl, pear)

PHP is a widely-used general-purpose scripting language that is especially \
suited for Web development and can be embedded into HTML.

Library Dependencies: libxml2, libxslt, openssl, tidy, zlib, libiconv, \
expat, gettext, tiff, mhash, libmcrypt, cclient, curl, jpeg, libpng, freetype
Platforms: darwin freebsd
Maintainers: ryandesign@macports.org jwa@macports.org
'''

    python25_teststring = '''python25 2.5, Revision 1, lang/python25 (Variants: darwin_8)
http://www.python.org/

Python is an interpreted, interactive, object-oriented programming language.

Platforms: darwin
Maintainers: mww@macports.org pmq@macports.org
'''

    missing_name_teststring = '''2.5, Revision 1, lang/python25 (Variants: darwin_8)
http://www.python.org/

Python is an interpreted, interactive, object-oriented programming language.

Platforms: darwin
Maintainers: mww@macports.org pmq@macports.org
'''

    missing_version_teststring = '''python25, Revision 1, lang/python25 (Variants: darwin_8)
http://www.python.org/

Python is an interpreted, interactive, object-oriented programming language.

Platforms: darwin
Maintainers: mww@macports.org pmq@macports.org
'''

    missing_directory_teststring = '''python25 2.5, Revision 1, (Variants: darwin_8)
http://www.python.org/

Python is an interpreted, interactive, object-oriented programming language.

Platforms: darwin
Maintainers: mww@macports.org pmq@macports.org
'''

class ParserTest(unittest.TestCase):
    def testTeTeXString(self):
        try:
            d = port_parser.parse_info(StaticStrings.tetex_teststring)
        except port_parser.ParseError:
            self.fail('Unexpected ParseError with tetex string')
        manual_dict = {
            PackageKey.NAME                   : 'teTeX',
            PackageKey.VERSION                : '3.0',
            PackageKey.REVISION               : '3',
            PackageKey.DIRECTORY              : 'print/teTeX',
            PackageKey.VARIANTS               : ['nox11'],
            PackageKey.HOMEPAGE               : 'http://www.tug.org/teTeX/',
            PackageKey.DESCRIPTION            : '''teTeX is a TeX distribution for \
UNIX compatible systems. It contains the latest versions of TeX & friends and \
nearly everything you need for happy TeX\'ing. For more information have a \
look at the lengthy FEATURES file of the distribution.''',
            PackageKey.BUILD_DEPENDENCIES     : ['gawk', 'gsed'],
            PackageKey.LIBRARY_DEPENDENCIES   : ['XFree86'],
            PackageKey.RUNTIME_DEPENDENCIES   : ['ghostscript', 'texinfo', 'texi2html'],
            PackageKey.PLATFORMS              : ['darwin'],
            PackageKey.MAINTAINERS            : ['gwright@opendarwin.org']      
        }
        
        for k in package.PackageKey:
            self.assertEquals(d.get(k), manual_dict.get(k))
            
    def testPHP5String(self):
        try:
            d = port_parser.parse_info(StaticStrings.php5_teststring)
        except port_parser.ParseError:
            self.fail('Unexpected ParseError with php5 string')
        manual_dict = {
            PackageKey.NAME                   : 'php5',
            PackageKey.VERSION                : '5.2.1',
            PackageKey.REVISION               : '',
            PackageKey.DIRECTORY              : 'www/php5',
            PackageKey.VARIANTS               : ['darwin_6', 'darwin_7', 'darwin_8', 'freebsd', 
                                                    'macosx', 'apache', 'apache2', 'fastcgi', 'snmp', 
                                                    'macports_snmp', 'mysql3', 'mysql4', 'mysql5', 
                                                    'postgresql', 'ipc', 'pcntl', 'pear'],
            PackageKey.HOMEPAGE               : '',
            PackageKey.DESCRIPTION            : '''PHP is a widely-used general-purpose scripting language that is especially \
suited for Web development and can be embedded into HTML.''',
            PackageKey.BUILD_DEPENDENCIES     : [],
            PackageKey.LIBRARY_DEPENDENCIES   : ['libxml2', 'libxslt', 'openssl', 'tidy', 'zlib', 
                                                    'libiconv', 'expat', 'gettext', 'tiff', 'mhash', 
                                                    'libmcrypt', 'cclient', 'curl', 'jpeg', 'libpng', 
                                                    'freetype'],
            PackageKey.RUNTIME_DEPENDENCIES   : [],
            PackageKey.PLATFORMS              : ['darwin', 'freebsd'],
            PackageKey.MAINTAINERS            : ['ryandesign@macports.org', 'jwa@macports.org']      
        }

        for k in package.PackageKey:
            self.assertEquals(d.get(k), manual_dict.get(k))

    def testPython25String(self):
        try:
            d = port_parser.parse_info(StaticStrings.python25_teststring)
        except port_parser.ParseError:
            self.fail('Unexpected ParseError with python25 string')
        manual_dict =     {
                PackageKey.NAME                 : 'python25',
                PackageKey.REVISION             : '1',
                PackageKey.VERSION              : '2.5',
                PackageKey.DIRECTORY            : 'lang/python25',
                PackageKey.HOMEPAGE             : 'http://www.python.org/',
                PackageKey.VARIANTS             : ['darwin_8'],
                PackageKey.DESCRIPTION          : 'Python is an interpreted, interactive, object-oriented programming language.',
                PackageKey.BUILD_DEPENDENCIES   : [],
                PackageKey.LIBRARY_DEPENDENCIES : [],
                PackageKey.RUNTIME_DEPENDENCIES : [],
                PackageKey.PLATFORMS            : ['darwin'],
                PackageKey.MAINTAINERS           : ['mww@macports.org', 'pmq@macports.org'] 
            }

        for k in package.PackageKey:
            self.assertEquals(d.get(k), manual_dict.get(k))
            
    def testIncompleteFirstLine(self):
        self.assertRaises(port_parser.ParseError, port_parser.parse_info, StaticStrings.missing_name_teststring)
        self.assertRaises(port_parser.ParseError, port_parser.parse_info, StaticStrings.missing_version_teststring)
        self.assertRaises(port_parser.ParseError, port_parser.parse_info, StaticStrings.missing_directory_teststring)
    
if __name__ == '__main__':
    unittest.main()